package ${classPackageNameMap[table.className+'ServiceImpl']};

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.page.PageMethod;
import com.jd.jxt.jxpp.wechat.robot.api.enums.YesOrNoEnum;
import ${classTypeNameMap[table.className+'VO']};
import ${classTypeNameMap[table.className+'Mapper']};
import ${classTypeNameMap[table.className]};
import ${classTypeNameMap[table.className+'Example']};
import ${classTypeNameMap[table.className+'Service']};
import com.jd.jxt.jxpp.wechat.robot.util.ParamUtil;
import com.jd.xtl.common.spi.response.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ${author!}
 * @since ${timeNow!}
 * ${table.remarks!}
 */
@Slf4j
@Service
public class ${table.className}ServiceImpl implements ${table.className}Service {
    @Resource
    private ${table.className}Mapper ${table.classNameFirstLower}Mapper;

    @Override
    public Long countByExample(${table.className}Example request) {
        log.info("${table.className}ServiceImpl.countByExample,查询数量,${table.className}:{}",JSON.toJSONString(request));
        if (request == null){
            request = new ${table.className}Example();
        }
        return ${table.classNameFirstLower}Mapper.countByExample(request);
    }
    @Override
    public ${table.className} selectByPrimaryKey(Long id) {
        log.info("${table.className}ServiceImpl.selectByPrimaryKey,通过id查询,id:{}",id);
        ParamUtil.notNull(id,"id");
        return ${table.classNameFirstLower}Mapper.selectByPrimaryKey(id);
    }
    @Override
    public List<${table.className}> selectByExample(${table.className}Example request) {
        log.info("${table.className}ServiceImpl.selectByExample,查询列表,${table.className}:{}",JSON.toJSONString(request));
        if (request == null){
            request = new ${table.className}Example();
        }
        return ${table.classNameFirstLower}Mapper.selectByExample(request);
    }
    @Override
    public List<${table.className+'VO'}> selectList(${table.className+'VO'} request) {
        log.info("${table.className}ServiceImpl.selectList,查询列表,${table.className+'VO'}:{}",request);
        if (request == null){
            request = new ${table.className+'VO'}();
        }
        request.setIsDeleted(YesOrNoEnum.NO.getCode());
        return ${table.classNameFirstLower}Mapper.selectList(request);
    }
    @Override
    public PageResult<${table.className+'VO'}> selectPage(${table.className+'VO'} request){
        log.info("${table.className}ServiceImpl.selectPage,查询列表,${table.className+'VO'}:{}",request);
        if (request == null){
            request = new ${table.className+'VO'}();
        }
        request.setIsDeleted(YesOrNoEnum.NO.getCode());
        PageMethod.startPage(request.getPageIndex(), request.getPageSize());
        List<${table.className+'VO'}> result = this.selectList(request);
        com.github.pagehelper.PageInfo<${table.className+'VO'}> pageInfo = new com.github.pagehelper.PageInfo<>(result);
        return PageResult.pageResult(pageInfo.getPageNum(),pageInfo.getPageSize(),(int)pageInfo.getTotal(),result);
    }
    @Override
    public Integer insert(${table.className} request) {
        log.info("${table.className}ServiceImpl.insert,新增,${table.className}:{}",request);
        ParamUtil.notNull(request,"要保存的对象");
        return ${table.classNameFirstLower}Mapper.insertSelective(request);
    }
    @Override
    public Integer insertBatch(List<${table.className+'VO'}> requests) {
        ParamUtil.notEmpty(requests,"要批量新增的List");
        log.info("${table.className}ServiceImpl.insertBatch,批量新增,${table.className+'VO'}:{}",requests.size());
        return ${table.classNameFirstLower}Mapper.insertBatch(requests);
    }
    @Override
    public Integer update(${table.className} request) {
        log.info("${table.className}ServiceImpl.update,更新,${table.className}:{}",request);
        ParamUtil.notNull(request,"要修改的对象");
        ParamUtil.notNull(request.getId(),"id");
        return ${table.classNameFirstLower}Mapper.updateByPrimaryKeySelective(request);
    }
    @Override
    public Integer deleteStateById(${table.className+'VO'} request){
        log.info("${table.className}ServiceImpl.deleteStateById,根据id删除,request:{}",request);
        ParamUtil.notNull(request,"request");
        ${table.className} entity = new ${table.className}();
        entity.setId(request.getId());
        entity.setIsDeleted(YesOrNoEnum.YES.getCode());
        return this.update(entity);
    }
    @Override
    public Integer delete(${table.className}Example request){
        log.info("${table.className}ServiceImpl.delete,根据条件删除,request:{}",JSON.toJSONString(request));
        ParamUtil.notNull(request,"删除条件");
        ParamUtil.notEmpty(request.getOredCriteria(),"删除条件");
        return ${table.classNameFirstLower}Mapper.deleteByExample(request);
    }
    @Override
    public Integer save(${table.className} param) {
        log.info("${table.className}ServiceImpl.save,保存,param:{}",param);
        ParamUtil.notNull(param,"保存对象");
        //this.checkParam(param);

        int updateNum;
        if(null==param.getId()){
            ${table.className} request =new ${table.className}();
            BeanUtils.copyProperties(param,request);
            updateNum = this.insert(request);
            param.setId(request.getId());
        }else{
            updateNum = this.update(param);
        }
        return updateNum;
    }
}